package com.dunqi.gpm.cangxi.home.adapter;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.base.RecyclerAdapter;
import com.dunqi.gpm.cangxi.common.util.CommonUtil;
import com.dunqi.gpm.cangxi.home.bean.Project;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.home.fragment.HomeFragment;
import com.dunqi.gpm.cangxi.home.widget.CircleTextView;
import com.dunqi.gpm.cangxi.mine.util.UserUtil;

import java.util.List;

/**
 * Created by 空 on 2017/11/17 0017.
 */

public class HomeProjectAdapter extends RecyclerAdapter<Project> {
    private final String[] status;

    public HomeProjectAdapter(@Nullable List<Project> data) {
        super(R.layout.item_home_project, data);
        status = Constance.STATUS;
    }

    @Override
    protected void convert(BaseViewHolder helper, Project item) {
        helper.setText(R.id.title_tv, helper.getAdapterPosition() + 1 + ". " + item.getProjectName())
                .setText(R.id.company_tv, item.getRespOrgName() + "")
                .setText(R.id.money_tv, item.getTotalInvestmentAmount() + "");

        TextView completedMoneyTv = helper.getView(R.id.completed_money_tv);

        double totalInvestmentAmount = item.getTotalInvestmentAmount();
        if (totalInvestmentAmount == 0) {
            completedMoneyTv.setText("");
        } else {
            double investedAmount = item.getTotalInvestedAmount();
         //   double percent = Arith.div(investedAmount * 100, totalInvestmentAmount, 2);
            completedMoneyTv.setText(investedAmount + "万元");
        }
        View plan_year_container = helper.getView(R.id.plan_year_ll);
        View state_container = helper.getView(R.id.state_ll);
        int auditStatus = item.getAuditStatus();
        if (auditStatus==1)
            helper.setTextColor(R.id.state_tv, CommonUtil.getColor(R.color.material_deep_orange_400));

        int type = HomeFragment.homeMenuId;
        int levelType = UserUtil.getLevelType();
        if (type == 12 && UserUtil.userFromNdrcOrg()) {
            plan_year_container.setVisibility(View.GONE);
            state_container.setVisibility(View.VISIBLE);
            helper.setText(R.id.state_tv, status[auditStatus]);
            return;
        }

//        int alarmStatus = item.getAlarmStatus();
//        CircleTextView topView = helper.getView(R.id.top_circle);
//        topView.setBgColor(Color.GRAY);
//        topView.setText(helper.getAdapterPosition()+"");
//        1-蓝 2-黄 3-红
//        if (alarmStatus == 1) {
//            topView.setBgColor(Color.BLUE);
//        } else if (alarmStatus == 2) {
//            topView.setBgColor(Color.YELLOW);
//        } else if (alarmStatus == 3) {
//            topView.setBgColor(Color.RED);
//        } else {
//
//        }

        if (auditStatus == 2 || auditStatus == 4) {
            plan_year_container.setVisibility(View.VISIBLE);
            state_container.setVisibility(View.GONE);
            CircleTextView circle1 = helper.getView(R.id.circle_1);
            CircleTextView circle2 = helper.getView(R.id.circle_2);
            CircleTextView circle3 = helper.getView(R.id.circle_3);
            circle1.setBgColor(Color.BLUE);
            circle2.setBgColor(Color.YELLOW);
            circle3.setBgColor(Color.RED);
            Integer alarmStatusBlue = item.getAlarmStatusBlue();
            Integer alarmStatusOrange = item.getAlarmStatusOrange();
            Integer alarmStatusRed = item.getAlarmStatusRed();

            alarmStatus(circle1, alarmStatusBlue);
            alarmStatus(circle2, alarmStatusOrange);
            alarmStatus(circle3, alarmStatusRed);

        } else {
            plan_year_container.setVisibility(View.GONE);
            state_container.setVisibility(View.VISIBLE);
            helper.setText(R.id.state_tv, status[auditStatus]);
        }
    }

    private void alarmStatus(CircleTextView circle, Integer alarmStatusBlue) {
        if (alarmStatusBlue == null || alarmStatusBlue == 0) {
            circle.setVisibility(View.GONE);
        } else {
            circle.setVisibility(View.VISIBLE);
            circle.setText("" + alarmStatusBlue);
        }
    }
}
